package engine.message;

import java.util.ArrayList;

public class MessageSystem {
	public MessageSystem() {
		_Queues = new ArrayList<MessageQueue>();
		_Broadcasters = new ArrayList<MessageBroadcaster>();
	}
	
	public boolean sendMessage(Message_A message) {
		return false;
	}
	public void addQueue(MessageQueue queue) {
		if(queue == null || _Queues.contains(queue)) {
			return;
		}
		_Queues.add(queue);
	}
	public void addBroadcaster(MessageBroadcaster broadcaster) {
		if(broadcaster == null || _Broadcasters.contains(broadcaster)) {
			return;
		}
		_Broadcasters.add(broadcaster);
	}
	public MessageBroadcaster getBroadcaster(Class<?> messageFamily) {
		for(MessageBroadcaster broadcaster : _Broadcasters) {
			if(broadcaster.MessageFamily == messageFamily) {
				return broadcaster;
			}
		}
		return null;
	}
	
	public MessageQueue getQueue(Class<?> messageFamily) {
		for(MessageQueue queue : _Queues) {
			if(queue.MessageFamily == messageFamily) {
				return queue;
			}
		}
		return null;
	}
	
	public boolean send(Message_A message) {
		MessageBroadcaster broadcaster = getBroadcaster(message.getClass());
		if(broadcaster == null) {
			return false;
		}
		broadcaster.broadcast(message);
		return false;
	}
	
	public boolean queue(Message_A message) {
		MessageQueue queue = getQueue(message.getClass());
		if(queue == null) {
			return false;
		}
		return queue.push(message);
	}
	
	private ArrayList<MessageQueue> _Queues;
	private ArrayList<MessageBroadcaster> _Broadcasters;
}
